<!-- 角色薪资编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="roleSalaryModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="roleSalaryModalApp.roleSalary"
        :rules="roleSalaryModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="员工角色:" name="role">
              <m-dict-select
                v-model:value="roleSalaryModalApp.roleSalary.role"
                placeholder="请选择角色"
                dict="员工角色"/>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="薪资:" name="salary">
              <a-input-number
                v-model:value="roleSalaryModalApp.roleSalary.salary"
                placeholder="请输入薪资"
                :min="0"
                :step="1"
                class="ele-fluid"
              ></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="roleSalaryModalApp.display">
                <teleport to="#roleSalaryEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="roleSalaryListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(roleSalaryModalApp.roleSalary)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!roleSalaryModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(roleSalaryModalApp.roleSalary)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {RoleSalaryService} from "@/views/restaurant/staff/roleSalary/roleSalaryService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';
import MDictSelect from "@/components/MDict/dictSelect";

export default defineComponent({
  components: {MDictSelect},
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let roleSalaryListApp = inject('roleSalaryListApp', reactive({}));
    let roleSalaryGrid=inject("roleSalaryGrid",reactive({}));
    const roleSalaryModalApp = reactive({
      id: null,
      roleSalary: {},
      isEdit: false,
      display: false
    });
    roleSalaryModalApp.rules = {
      role: [
        {
          required: true,
          type: 'string',
          message: '请输入员工角色',
          trigger: 'blur'
        }
      ],
      salary: [
        {
          required: true,
          type: 'number',
          message: '请输入薪资',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      roleSalaryModalApp.roleSalary = reactive({});
      if (routeId) {//路由
        roleSalaryModalApp.id = routeId;
        roleSalaryModalApp.display = false;
        findRoleSalary(roleSalaryModalApp.id);
      } else if (roleSalaryListApp.editModalForEdit) {//修改弹窗
        roleSalaryModalApp.id = roleSalaryListApp.currentId;
        roleSalaryModalApp.display = true;
      } else if (!roleSalaryListApp.editModalForEdit && roleSalaryListApp.addModalForEdit) {//新增弹窗
        roleSalaryModalApp.display = true;
      } else {
        roleSalaryModalApp.title = "新增";
      }
      if (roleSalaryModalApp.id) findRoleSalary(roleSalaryModalApp.id);
    })

    const findRoleSalary = (id) => {
      RoleSalaryService.findRoleSalaryForView(id).then((res) => {
        roleSalaryModalApp.roleSalary = res.data;
        //TODO:为编辑准备.
        // roleSalaryModalApp.title = "编辑 " + roleSalaryModalApp.roleSalary.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        RoleSalaryService.updateRoleSalary(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        RoleSalaryService.saveRoleSalary(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/restaurant/staff/roleSalary`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        roleSalaryGrid.value.commitProxy("query");
      }, 1500);
      roleSalaryListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      roleSalaryListApp,
      roleSalaryModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
